<template>
  <div class="customer-consultation">
    <div class="customer-consultation-header">
      <div class="customer-consultation-title">咨询客户</div>
      <ElSelect v-model="queryCycleType" style="width: 80px;" @change="onChange">
        <ElOption label="日" :value="1"/>
        <ElOption label="周" :value="2"/>
        <ElOption label="月" :value="3"/>
        <ElOption label="年" :value="4"/>
      </ElSelect>
    </div>

    <ElTabs v-model="type" @tab-change="onChange(queryCycleType)">
      <ElTabPane
        v-for="(item, index) in tabs"
        :key="index"
        :label="item.label"
        :name="item.value"
      ></ElTabPane>
    </ElTabs>

    <ElForm class="customer-consultation-form">
      <ElFormItem label="日期">
        <ElDatePicker
          v-model="date"
          format="YYYY-MM-DD"
          valueFormat="YYYY-MM-DD"
          type="daterange"
          :clearable="true"
        />
      </ElFormItem>
      <ElFormItem style="margin-left: 16px;">
        <ElButton style="width: 80px;" type="primary" @click="onChange(queryCycleType, true)">查询</ElButton>
      </ElFormItem>
    </ElForm>

    <ElTable v-loading="loading" :data="list" max-height="226">
      <ElTableColumn type="index" label="#" :width="50" align="center" />
      <ElTableColumn prop="date" label="日期" />
      <ElTableColumn prop="value" label="值" />
      <ElTableColumn prop="value" label="环比">
        <template #="{ row }">
          {{ row.qoq || 0 }}%
        </template>
      </ElTableColumn>
      <ElTableColumn prop="value" label="同比">
        <template #="{ row }">
          {{ row.yoy || 0 }}%
        </template>
      </ElTableColumn>
    </ElTable>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import dayjs from 'dayjs'
import { getStatistics5 } from '../api'

const type = ref(1)
const tabs = computed(() => {
  return [
    { label: '咨询客户数', value: 1 },
    { label: '跟进客户数', value: 2 },
    { label: '无效客户数', value: 3 },
    { label: '成交客户数', value: 4 }
  ]
})

const queryCycleType = ref(1)
const date = ref([])

const loading = ref(false)
const list = ref([])

const changeDate = (val: number) => {
  // const dateEnd = dayjs().format('YYYY-MM-DD')
  // let dateStart
  // if (val === 1) {
  //   dateStart = dayjs().subtract(30, 'day').format('YYYY-MM-DD')
  // } else if (val === 2) {
  //   dateStart = dayjs().subtract(13, 'week').format('YYYY-MM-DD')
  // } else if (val === 3) {
  //   dateStart = dayjs().subtract(12, 'month').format('YYYY-MM-DD')
  // } else if (val === 4) {
  //   dateStart = dayjs().subtract(3, 'year').format('YYYY-MM-DD')
  // }
  date.value = []
}

const onChange = (val, flag = false) => {
  loading.value = true
  queryCycleType.value = val
  if (!flag) {
    changeDate(val)
  }
  getStatistics5({queryCycleType: queryCycleType.value, dateStart: date.value[0], dateEnd: date.value[1], type: type.value})
    .then(res => {
      list.value = res || []
    })
    .finally(() => {
      loading.value = false
    })
}
onMounted(() => {
  onChange(1)
})
</script>

<style scoped lang="scss">
.customer-consultation {
  height: 400px;
  padding: 20px;
  box-sizing: border-box;
  &-title {
    font-family: PingFangSC-Medium;
    font-weight: 500;
    font-size: 16px;
    color: #333333;
    margin-bottom: 16px;
  }
  &-header {
    font-family: PingFangSC-Medium;
    font-weight: 500;
    font-size: 16px;
    color: #333333;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  &-form {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 6px;
  }

  :deep(.el-date-editor.el-input__wrapper) {
    width: auto;
  }
  :deep(.el-form-item) {
    margin-bottom: 10px;
  }
}
</style>
